package com.uh.summary;

import com.uh.calculation.bean.OperatorVal;
import com.uh.calculation.bean.Operators;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.StringReader;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;

public class test {


    public static void main(String[] args) throws Exception {
        readXml();
    }



    public  static void readXml() throws Exception{



        String path = ResourceUtils.getURL("classpath:calculation/operator.xml").getPath();
        File file = new File(path);
        SAXReader saxReader = new SAXReader();

        JAXBContext  context = JAXBContext.newInstance(Operators.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        Object object = unmarshaller.unmarshal(file);
        Operators reciever = (Operators)object;

        List<OperatorVal>  operatorVal = reciever.getOperatorVal();

        OperatorVal first = operatorVal.stream().filter(OperatorVal -> Objects.equals(OperatorVal.getName(),1)).findFirst().get();

        List<OperatorVal>  firsts = operatorVal.stream().filter(OperatorVal -> Objects.equals(OperatorVal.getName(),1)).collect(Collectors.toList());




        System.out.println("");













        //2.加载xml
       /* Document document = saxReader.read(file);
        //3.获取根节点
        Element rootElement = document.getRootElement();
        // rootElement.elements()获取根节点下所有的节点，
        List<Element> elements = rootElement.elements();
        //获取节点book1下属性为id的值 003
        rootElement.element("book1").attribute("id").getValue();

        for (Element element : elements) {
            //依次遍历根节点下的节点  book book book1
            element.getName();
            //依次遍历根节点下的节点对应属性为id的值 001 002 003
            System.out.println(element.attribute("id").getValue());
            System.out.println("----------------------------");
            //同样道理 继续依次获取该节点下的节点
            List<Element> child = element.elements();
            for (Element element1 : child) {
                //依次/获取子节点  id title author   、title  author、title author
                element1.getName();
                //依次获取子节点的值 9  Harry Potter  J K. Rowling、.......
                System.out.println(element1.getText());
            }
        }
        // 获取第二个节点book的属性为id的值 002
        elements.get(1).attribute("id").getValue();
        // 获取根节点下第二个节点book所有节点
        List<Element> elements1 = elements.get(1).elements();
        //获取根节点下第二个节点book节点下的第二个节点author的值 Erik T. Ray
        elements.get(1).elements().get(1).getText();*/
    }

















}